package com.ash.nio;

import java.nio.ByteBuffer;

public class BuffUtil {

    public static String byteBuffString(ByteBuffer byteBuffer) {
        // 确保是读的模式
        byteBuffer.flip();
        // 获取剩余可读字节数
        int remaining = byteBuffer.remaining();
        byte[] bytesWithoutZeroes = new byte[remaining];
        int destPos = 0;

        while (byteBuffer.hasRemaining()) {
            byte b = byteBuffer.get();

            bytesWithoutZeroes[destPos++] = b;

        }

        return new String(bytesWithoutZeroes);
    }
}
